﻿@model Serene.Navigation.NavigationModel

@functions {
    public HtmlString renderItem(Serenity.Navigation.NavigationItem item, int depth, int[] path) {

        var isactive = true;
        for (var i = 0; i <= depth; i++)
        {
            if (path[i] != Model.ActivePath[i])
            {
                isactive = false;
                break;
            }
        }

        var klass = (isactive ? "active" : "") + (item.Children.IsEmptyOrNull() ? "" : " treeview");
        if (!string.IsNullOrEmpty(item.ItemClass))
        {
            klass = (klass + " " + item.ItemClass).Trim();
        }
        var icon = (item.IconClass ?? (item.Children.Count > 0 ? "icon-layers" : (depth == 0 ? "icon-link" : "fa-circle-o")));
        var title = (LocalText.TryGet("Navigation." + item.FullPath) ?? item.Title);
        var sectionUrl = (item.Url ?? "javascript:;");
        var target = item.Target; if (target != null) { target += " target=" + target; }

        var sb = new System.Text.StringBuilder();
        sb.Append("<li class='" + klass + "'><a href='" + HtmlEncoder.Encode(sectionUrl) + "' " + target + ">" +
        "<i class='fa " + icon + "'></i><span>" + HtmlEncoder.Encode(title) + "</span>" + (!item.Children.IsEmptyOrNull() ? "<i class='fa fa-angle-left pull-right'></i>" : "") + "</a>");

        if (item.Children.Count > 0) {
            sb.Append("<ul class='treeview-menu'>");
            path[depth + 1] = 0;
            foreach (var child in item.Children)
            {
                sb.Append(renderItem(child, depth + 1, path).ToString());

            }
            sb.Append("</ul>");
        }
        sb.Append("</li>");
        path[depth]++;
        return new HtmlString(sb.ToString());
    }
}

@{var path = new int[10]; }
@foreach (var item in Model.Items)
{
    @renderItem(item, 0, path);
}

<script type="text/javascript">
    function openWindow(url, width, height) {
        height = height || (screen.availHeight - 60);
        width = width || (screen.availWidth - 10);
        var x = (screen.availWidth - width) / 2 - 5; if (x < 0) x = 0;
        var y = (screen.availHeight - height) / 2 - 25; if (y < 0) y = 0;
        var winPopup = window.open(url, "", "status=0, toolbar=0, width=" + width + ", height=" + height +
            ", scrollbars=1, resizable=yes, left=" + x + ", top=" + y);
    }
</script>